/*
 * @lc app=leetcode id=141 lang=cpp
 *
 * [141] Linked List Cycle
 */
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    bool hasCycle(ListNode *head) {
        if (!head) {
            return false;
        }

        ListNode *fast = head->next;
        ListNode *slow = head;

        while(fast && slow) {
            if (fast == slow) {
                return true;
            }

            if (fast->next) {
                fast = fast->next->next;
            } else {
                return false;
            }

            slow = slow->next;
        }
        return false;
    }
};

